package cn.zhejianglao.shirotemplate.service.role;

import cn.zhejianglao.shirotemplate.common.model.Response;
import cn.zhejianglao.shirotemplate.common.model.PagedResult;
import cn.zhejianglao.shirotemplate.common.model.RoleVO;
import cn.zhejianglao.shirotemplate.common.validator.ParamValidator;
import cn.zhejianglao.shirotemplate.common.model.UserRoleVO;
import cn.zhejianglao.shirotemplate.service.role.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequiresRoles("admin")
    @PostMapping("/create")
    public Response<String> create(@RequestBody RoleVO roleVO) {
        ParamValidator.validate(roleVO);
        roleService.create(roleVO);
        return Response.success();
    }

    @RequiresRoles("admin")
    @DeleteMapping("/delete/{id}")
    public Response<String> deleteRole(@PathVariable("id") Long roleId) {
        roleService.deleteRole(roleId);
        return Response.success();
    }

    @RequiresRoles("admin")
    @GetMapping("/list/{size}/{page}")
    public Response<PagedResult<RoleVO>> listRole(@PathVariable("size") int size, @PathVariable("page") int page) {
        PagedResult<RoleVO> result = roleService.listRoles(size, page);
        return Response.success(result);
    }

    @RequiresRoles("admin")
    @PostMapping("/update")
    public Response<String> updateRole(@RequestBody RoleVO roleVO) {
        ParamValidator.validate(roleVO);
        roleService.updateRole(roleVO);
        return Response.success();
    }

    @RequiresRoles("admin")
    @PostMapping("/authorize")
    public Response<String> authorize(@RequestBody UserRoleVO request) {
        ParamValidator.validate(request);
        roleService.authorize(request);
        return Response.success();
    }

    @RequiresRoles("admin")
    @PostMapping("/cancelAuthorization")
    public Response<String> cancelAuthorization(@RequestBody UserRoleVO request) {
        ParamValidator.validate(request);
        roleService.cancelAuthorization(request);
        return Response.success();
    }
}
